﻿using UnityEditor;
using UnityEditor.UI;
using UnityEngine;

namespace CGF
{
    [CustomEditor(typeof(ImagePro))]
    public class ImageProEditor : ImageEditor
    {
        ImagePro img;

        protected override void OnEnable()
        {
            base.OnEnable();
            img = (ImagePro)target;
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            
            EditorGUILayout.Space();
            
            img.textureScale = EditorGUILayout.FloatField("Texture Scale", img.textureScale);
            if (img.textureScale < 0) img.textureScale = 0;

            EditorGUILayout.Space();
            
            img.cornerRadius = EditorGUILayout.FloatField("Corner Radius", img.cornerRadius);
            if (img.cornerRadius < 0) img.cornerRadius = 0;
            float halfSizeX = img.rectTransform.rect.width * 0.5f;
            float halfSizeY = img.rectTransform.rect.height * 0.5f;
            float halfSizeMin = halfSizeX > halfSizeY ? halfSizeY : halfSizeX;
            if (img.cornerRadius > halfSizeMin) img.cornerRadius = halfSizeMin;
            if (img.cornerRadius > 0)
            {
                img.cornerDetail = EditorGUILayout.IntField("Corner Detail", img.cornerDetail);
                if (img.cornerDetail < 1) img.cornerDetail = 1;
                if (img.cornerDetail > 25) img.cornerDetail = 25;
            }
            
            EditorGUILayout.Space();
            
            img.borderWidth = EditorGUILayout.FloatField("Border Width", img.borderWidth);
            if (img.borderWidth < 0) img.borderWidth = 0;
            if (img.borderWidth > 0)
            {
                img.borderColor = EditorGUILayout.ColorField("Border Color", img.borderColor);
            }
            
            EditorGUILayout.Space();

            img.gradient = EditorGUILayout.Toggle("Gradient", img.gradient);
            if (img.gradient)
            {
                img.isVerticalGradient = EditorGUILayout.Toggle("    Vertical Gradient", img.isVerticalGradient);
                img.beginColor = EditorGUILayout.ColorField("    Start Color", img.beginColor);
                img.endColor = EditorGUILayout.ColorField("    End Color", img.endColor);
                EditorGUILayout.Space();
            }

            img.effect = (ImageProEffect)EditorGUILayout.EnumPopup("Effect", img.effect);
            if (img.effect == ImageProEffect.FlowLight)
            {
                img.flowLightTexture = (Texture)EditorGUILayout.ObjectField("    Light Texture", img.flowLightTexture, typeof(Texture), false);
                img.flowLightLightScale = EditorGUILayout.FloatField("    Light Scale", img.flowLightLightScale);
                img.flowLightIntensity = EditorGUILayout.FloatField("    Intensity", img.flowLightIntensity);
                img.flowLightSpeed = EditorGUILayout.FloatField("    Speed", img.flowLightSpeed);
                img.flowLightAngle = EditorGUILayout.FloatField("    Angle", img.flowLightAngle);
            }

            if (GUI.changed)
            {
                serializedObject.ApplyModifiedProperties();
                EditorUtility.SetDirty(target);
                img.SetAllDirty();
            }
        }
    }
}